package com.cdut.drivingschool.ui.main;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;

import com.cdut.drivingschool.R;
import com.cdut.drivingschool.adapter.SchoolListAdapter;
import com.cdut.drivingschool.bean.BaseType;
import com.cdut.drivingschool.bean.Group;
import com.cdut.drivingschool.bean.School;
import com.cdut.drivingschool.config.Key;
import com.cdut.drivingschool.http.HttpClient;
import com.cdut.drivingschool.http.parser.SchoolParser;
import com.cdut.drivingschool.listener.HttpListener;
import com.cdut.drivingschool.util.ActivityUtil;
import com.cdut.drivingschool.util.AppUtil;
import com.cdut.drivingschool.view.LoadingView;
import com.cdut.drivingschool.view.XListView;
import com.cdut.drivingschool.view.XListView.IXListViewListener;
/**
 * 首页
 * @author Administrator
 *
 */
public class HomeFragment extends BaseFragment implements IXListViewListener{
	private View mView;
	private XListView mSchoolList;
	private SchoolListAdapter mAdapter;
	private Group<School> mGroup;
	private LoadingView mLoading;
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		mView = inflater.inflate(R.layout.fragment_home, container,false);
		mSchoolList = (XListView) mView.findViewById(R.id.school_list);
		mSchoolList.setPullLoadEnable(false);
		mSchoolList.setPullRefreshEnable(false);
		mSchoolList.setXListViewListener(this);
		mAdapter = new SchoolListAdapter(getActivity());
		mSchoolList.setAdapter(mAdapter);
		mLoading = (LoadingView) mView.findViewById(R.id.loading);
		mLoading.showLoading("数据加载中...");
		mGroup = new Group<School>();
		getSchoolList();
		return mView;
	}
	public static HomeFragment newInstance(){
		return new HomeFragment();
	}
	/**
	 * 获取驾校列表
	 */
	public void getSchoolList(){
		HttpClient.request("get_schoolfirstpage_one.php", null, new SchoolParser(), new HttpListener() {
			@Override
			public void onSuccess(BaseType data) {
				 mGroup = (Group<School>) data;
				 mSchoolList.stopRefresh();
				 mSchoolList.setPullRefreshEnable(true);
				 mAdapter.setGroup(mGroup);
				 mLoading.stopLoading();
			}
			@Override
			public void onFail(String erro_msg) {
				mLoading.showLoadingFail(erro_msg);
			}
		});
	}
	@Override
	public void onRefresh() {
		getSchoolList();
	}
	@Override
	public void onLoadMore() {
	}
	@Override
	public void onClickItem(int position) {
		/*Bundle bd = new Bundle();
		bd.putInt(Key.SCHOOL_ID, mGroup.get(position).school_id);
		ActivityUtil.next(getActivity(), SchoolDetailActivity.class, bd, 0);*/
	}
}
